import 'dart:async';
import 'dart:collection';
// In order to *not* need this ignore, consider extracting the "web" version
// of your plugin as a separate package, instead of inlining it in the same
// package as the core of your plugin.
// ignore: avoid_web_libraries_in_flutter
//import 'dart:html' as html show window;
import 'dart:html';

import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

import 'bean/loc_data_bean.dart';

/// A web implementation of the HuiLocation plugin.
class HuiLocationWeb {
  static void registerWith(Registrar registrar) {
    final MethodChannel channel = MethodChannel(
      'hui_location',
      const StandardMethodCodec(),
      registrar,
    );

    final pluginInstance = HuiLocationWeb();
    channel.setMethodCallHandler(pluginInstance.handleMethodCall);
  }

  /// Handles method calls over the MethodChannel of this plugin.
  /// Note: Check the "federated" architecture for a new way of doing this:
  /// https://flutter.dev/go/federated-plugins
  Future<dynamic> handleMethodCall(MethodCall call) async {
    switch (call.method) {
      case 'getPlatformVersion':
        return getPlatformVersion1();
      default:
        throw PlatformException(
          code: 'Unimplemented',
          details: 'hui_location for web doesn\'t implement \'${call.method}\'',
        );
    }
  }

  ///web端获取位置信息
  Future<Map<String, double>?> getPlatformVersion() async {
   final Geoposition? version =
        await window.navigator.geolocation.getCurrentPosition();
    Coordinates? coordinates = version!.coords;
    //print(version.coords!.latitude!.toString());

   HashMap<String, double> loc = HashMap();
   loc['latitude'] = coordinates!.latitude!.toDouble();
   loc['longitude'] = coordinates.longitude!.toDouble();
   loc['accuracy'] = coordinates.accuracy!.toDouble();
   loc['altitude'] = coordinates.altitude!.toDouble();
   loc['speed'] = coordinates.speed!.toDouble();
   loc['speedAccuracy'] = coordinates.altitudeAccuracy!.toDouble();

    return loc;
  }

  Future<String?> getPlatformVersion1() async {
    final Geoposition? version = await window.navigator.geolocation.getCurrentPosition();
    if(version == null){
      print('位置信息获取失败');
    }
    print(version.toString());
    return Future.value('${version!.coords!.longitude.toString()};${version.coords!.latitude.toString()}');
  }
}
